﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Administrator_ctTinTuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] == null)
        {
            this.FormView1.DefaultMode = FormViewMode.Insert;
        }
    }
    protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        FileUpload fu = (FileUpload)this.FormView1.FindControl("UpdateAnh");
        if (fu.HasFile)
        {
            string type = fu.FileName.Substring(fu.FileName.Length - 3, 3);
            TinTucDA kda = new TinTucDA();
            string name = RandomNumber().ToString();
            string pat = Server.MapPath("~/Image/TinTuc/" +name  + "." + type);
            fu.SaveAs(pat);
            kda.updateImagebyID(type, name, Convert.ToInt32(Request.QueryString["ID"]));

        }
    }
    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        FileUpload fu = (FileUpload)this.FormView1.FindControl("InsertAnh");
        if (fu.HasFile)
        {
            string type = fu.FileName.Substring(fu.FileName.Length - 3, 3);
            TinTucDA kda = new TinTucDA();
            string name = RandomNumber().ToString();
            string pat = Server.MapPath("~/Image/TinTuc/" + name + "." + type);
            fu.SaveAs(pat);
            kda.updateImage(type,name);
            Response.Redirect("TinTuc.aspx");
            
        }
    }
    private int RandomNumber()
    {
        Random random = new Random();
        return random.Next(1, 1000000000);
    }
    protected void InsertCancelButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("TinTuc.aspx");
    }
    protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        
    }
}